package com.xxxx.yebserver.controller;


import com.xxxx.yebserver.common.R;
import com.xxxx.yebserver.entity.Department;
import com.xxxx.yebserver.service.DepartmentService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author cxq
 * @since 2021-06-15
 */
@RestController
@RequestMapping("/system/basic/department")
public class DepartmentController {
    @Autowired
    DepartmentService departmentService;

    @ApiOperation("获取所有部门")
    @GetMapping("/")
    public List<Department> getAllDepartment(){
        return departmentService.list();
    }

    @ApiOperation("获取所有分级后的部门")
    @GetMapping("/tree")
    public List<Department> getTreeDepartment(){
        return departmentService.getTreeDepartment();
    }

    @ApiOperation("添加部门")
    @PostMapping("/")
    public R getAllDepartment(@RequestBody Department department){
        boolean save = departmentService.saveDep(department);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("修改部门")
    @PutMapping("/")
    public R updateDepartment(@RequestBody Department department){
        boolean save = departmentService.updateById(department);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("删除部门")
    @DeleteMapping("/{id}")
    public R deleteDepartment(@PathVariable("id") Long id){
        boolean save = departmentService.deleteDep(id);
        if(save) return R.ok();
        return R.error();
    }

    @ApiOperation("批量删除部门")
    @DeleteMapping("/")
    public R deleteAllDepartment(@RequestBody Long[] ids){
        boolean save = departmentService.removeByIds(Arrays.asList(ids));
        if(save) return R.ok();
        return R.error();
    }
}

